package com.mtt.erp.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.mtt.erp.common.domain.QueryRequest;
import com.mtt.erp.common.exception.MttErpException;
import com.mtt.erp.common.utils.MttErpUtil;
import com.mtt.erp.common.utils.SortUtil;
import com.mtt.erp.common.utils.UUIDUtils;
import com.mtt.erp.logistics.domain.OrderSenderInfo;
import com.mtt.erp.logistics.dao.OrderSenderInfoMapper;
import com.mtt.erp.logistics.dto.FaceOrderPerson;
import com.mtt.erp.logistics.dto.OrderSenderInfoCreateDto;
import com.mtt.erp.logistics.dto.OrderSenderInfoDto;
import com.mtt.erp.logistics.dto.OrderSenderInfoUpdateDto;
import com.mtt.erp.logistics.service.IOrderSenderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mtt.erp.logistics.vo.OrderSenderInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @author Lynne
 */
@Service
public class OrderSenderInfoServiceImpl extends ServiceImpl<OrderSenderInfoMapper, OrderSenderInfo> implements IOrderSenderInfoService {

    @Override
    public IPage<OrderSenderInfoVo> getOrderSenderInfo(QueryRequest request, OrderSenderInfoDto orderSenderInfoDto) {
        LambdaQueryWrapper<OrderSenderInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(orderSenderInfoDto.getSendName())) {   //查询条件是发货人
            queryWrapper.eq(OrderSenderInfo::getSendName, orderSenderInfoDto.getSendName());
        }
        queryWrapper.eq(OrderSenderInfo::getCuser, MttErpUtil.getCurrentUser().getUserId());
        Page<OrderSenderInfo> page = new Page<>(request.getPageNum(), request.getPageSize());
        SortUtil.handlePageSort(request, page, true);

        List<OrderSenderInfo> orderSenderInfolist = this.page(page, queryWrapper).getRecords();
        Page<OrderSenderInfoVo> pageVo = new Page<>(request.getPageNum(), request.getPageSize());//将发货人信息拷到Vo
        List<OrderSenderInfoVo> orderSenderInfoVoList = Lists.newArrayList();
        if(!orderSenderInfolist.isEmpty()){
            for(OrderSenderInfo orderSenderInfo : orderSenderInfolist){
                OrderSenderInfoVo orderSenderInfoVo = OrderSenderInfoVo.builder().build();
                BeanUtils.copyProperties(orderSenderInfo,orderSenderInfoVo);
                orderSenderInfoVoList.add(orderSenderInfoVo);
            }
        }
        BeanUtils.copyProperties(page,pageVo);
        pageVo.setRecords(orderSenderInfoVoList);
        return pageVo;
    }
    @Override
    @Transactional
    public void createOrderSenderInfo(@Valid OrderSenderInfoCreateDto orderSenderInfoCreateDto){
        OrderSenderInfo orderSenderInfo= OrderSenderInfo.builder().build();
        BeanUtils.copyProperties(orderSenderInfoCreateDto,orderSenderInfo);
        orderSenderInfo.setId(UUIDUtils.getUUID());
        orderSenderInfo.setCdate(LocalDateTime.now());
        Long currentUserId = MttErpUtil.getCurrentUser().getUserId();
        orderSenderInfo.setCuser(currentUserId.toString());
        this.save(orderSenderInfo);
    }
    @Override
    @Transactional
    public void updateOrderSenderInfo(@Valid OrderSenderInfoUpdateDto orderSenderInfoUpdateDto){
        OrderSenderInfo orderSenderInfo= OrderSenderInfo.builder().build();
        BeanUtils.copyProperties(orderSenderInfoUpdateDto,orderSenderInfo);
        Long currentUserId = MttErpUtil.getCurrentUser().getUserId();
        orderSenderInfo.setMdate(LocalDateTime.now());
        orderSenderInfo.setMuser(currentUserId.toString());
        this.updateById(orderSenderInfo);
    }
    @Override
    @Transactional
    public void deleteOrderSenderInfo(String[] orderSenderInfoIds) throws MttErpException {
        this.removeByIds(Arrays.asList(orderSenderInfoIds));
    }

    @Override
    public OrderSenderInfoVo getOrderSenderInfoById(String id) {
        OrderSenderInfoVo orderSenderInfoVo = OrderSenderInfoVo.builder().build();
        OrderSenderInfo orderSenderInfo = this.getById(id);
        if (orderSenderInfo != null) {
            BeanUtils.copyProperties(orderSenderInfo, orderSenderInfoVo);
        } else {
            return null;
        }
        return orderSenderInfoVo;
    }
}


